<?php

namespace app\api\model;

use app\api\model\Common;
use think\Validate;
use think\Db;

class Version extends Common
{
    //设置规则
    protected $rules = [
        'version' => 'require',
    ];
    /**
     *  查询数据列表
     *  参数
     *  param 符合数据库属性的数组，默认为空
     *  limit  条数
     *  page   分页页数
     */
    public function getDate($param,  $limit = '1', $page = '1')
    {
        try {
            $result = $this->allowField(true)->where($param)->limit($limit)->page($page)->order('version_id desc')->select();
            return $result;
        } catch (\Exception $e) {
            //throw new \Exception("查找失败");
            return false;
        }
    }

    /**
     * 插入数据
     * @rule   规则
     * @param  参数
     */
    public function createDates($param)
    {
        // 验证
        $validate = new Validate($this->rules);
        if (!$validate->check($param)) {
            $this->error = $validate->getError();
            return false;
        }
        try {
            $this->allowField(true)->data($param)->save();
            return true;
        } catch (\Exception $e) {
            $this->error = '插入失败';
            return false;
        }
    }

    /**
     * 创建群组，关联用户
     */
    public function equsToVersion($eids = array())
    {
        try {
            Db::startTrans();
            //获取一条最新的数据
            $result = $this->order('version_id desc')->limit(1)->find();
            if (empty($result)) {
                $this->error = "未找到最近的安装包,请上传最新安装包";
                return false;
            }

            $ev_model = Model('EquipmentVersion');
            foreach ($eids as &$param) {
                $param = [
                    'vid' => $result->version_id,
                    'eid' => $param
                ];
            }
            $ev_model->InsertAll($eids);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            $this->error = '创建失败';
            return false;
        }
    }

    /**
     * 
     */
    public function checkVersion($version)
    {
        $_last = $this->order('version_id desc')->limit(1)->find();
        if ($_last->version == $version) {
            $_last->is_force = 0;
            return [1, $_last];
        }
        //检查
        $_version = $this->get(['version' => $version]);
        if ($_version) {
            //检查错过版本号
            $_versioned = $this->where($this->getPk(), '>', $_version->version_id)->select();

            foreach ($_versioned as $value) {
                if ($value->is_force) {
                    $_last->is_force = 1;
                    return [1, $_last];
                }
            }
        }

        return [0, $_last];
    }

    /**
     * 
     */
    public function checkVersionCode($versionCode, $type)
    {
        $_last = $this->where(['device_type' => $type])->order('version_id desc')->limit(1)->find();
        //最新版本就不升级
        if ($_last->code == $versionCode) {
            $_last->is_force = 0;
            $_last->status = 0;
            return $_last;
        }
        //检查，前几个版本是否有强制升级版本，有就强制升级
        //检查错过版本号
        $_versioned = $this->where('code','>', $versionCode)->where('device_type', $type)->select();
        if ($_versioned) {
            foreach ($_versioned as $value) {
                if ($value->is_force) {
                    $_last->is_force = 1;
                    $_last->status = 1;
                    return $_last;
                }
            }
        }

        //前几个版本没有强制升级，且版本低于之前版本，则提示可升级
        $_last->is_force = 0;
        $_last->status = 1;
        return $_last;
    }

    /**
     * [updateDataById 编辑]
     * @linchuangbin
     * @DateTime  2017-02-10T21:24:49+0800
     * @param     [type]                   $param [description]
     * @param     [type]                   $id    [description]
     * @return    [type]                          [description]
     */
    public function updateDataById($param, $id)
    {
        $checkData = $this->get($id);
        if (!$checkData) {
            $this->error = '暂无此数据';
            return false;
        }

        // 验证
        // $validate = validate($this->name);
        // if (!$validate->check($param)) {
        // 	$this->error = $validate->getError();
        // 	return false;
        // }

        try {
            $this->allowField(true)->save($param, [$this->getPk() => $id]);
            return true;
        } catch (\Exception $e) {
            $this->error = '编辑失败';
            return false;
        }
    }
}
